package com.chenyue.cm.api.account;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.chenyue.cm.account.domain.JdAccount;
import com.chenyue.cm.account.service.JdAccountRecordService;
import com.chenyue.cm.account.service.JdAccountService;
import com.chenyue.cm.account.vo.SignInfo;
import com.chenyue.cm.auth.Login;
import com.chenyue.cm.auth.LoginUser;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.user.domain.JdAppUser;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author libingxing
 */
@Api(tags = "签到")
@RestController
@RequestMapping("/api/sign")
public class ApiSignController {

    private final JdAccountService accountService;

    private final JdAccountRecordService accountRecordService;

    public ApiSignController(JdAccountService accountService, JdAccountRecordService accountRecordService) {
        this.accountService = accountService;
        this.accountRecordService = accountRecordService;
    }

    @Login
    @PostMapping("/sign")
    @ApiOperation("每日签到")
    @ApiOperationSupport(order = 1)
    public Success<String> sign(
            @ApiIgnore @LoginUser JdAppUser user
    ) {
        JdAccount amount = accountService.getAmount(Integer.valueOf(user.getId()));
        return accountRecordService.sign(Integer.valueOf(user.getId()), amount);
    }

    @Login
    @GetMapping("/getSignRecord")
    @ApiOperation("/获取签到记录")
    public Success<SignInfo> getSignRecord(
            @ApiIgnore @LoginUser JdAppUser user
    ) {
        // 累计签到
        SignInfo signInfo = accountRecordService.getSign(Integer.valueOf(user.getId()));
        return Success.success(signInfo);
    }
}
